package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.domain.ConsumableStorage;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.service.ConsumableStorageService;
import com.hushao.lims.weblims.utils.UserUtil;
import com.hushao.lims.weblims.vo.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 930588706
 * @date 18/1/22
 */
@Controller
@RequestMapping("/consumableStorage")
public class ConsumableStorageController {
    @Autowired
    private ConsumableStorageService consumableStorageService;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private UserUtil userUtil;

    /**
     * 查询耗材入库
     * @param consumableName
     * @param state
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "耗材入库列表（分页）")
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @GetMapping
    public String listConsumableStorage(@RequestParam(value = "consumableName",required = false,defaultValue = "") String consumableName,
                                        @RequestParam(value = "state",required = false,defaultValue = "") String state,
                                        @RequestParam(value = "async",required = false) boolean async,
                                        Pageable pageable,
                                        Model model)throws Exception{
        TeacherInfo teacher= userUtil.isLogged(SecurityContextHolder.getContext().getAuthentication());

        Page<ConsumableStorage> consumableStoragePage=consumableStorageService.listConsumableStorageByConsumableNameLikeAndStateLike(consumableName,state,pageable);
        List<ConsumableStorage> consumableStorages=consumableStoragePage.getContent();
        model.addAttribute("page",consumableStoragePage);
        model.addAttribute("consumableStorages",consumableStorages);
        return async==true?"/audit/consumableStorages ::#mainContainerRepleace":"/audit/consumableStorages";
    }

    /**
     * 根据耗材名称模糊查询个人耗材入库
     * @param consumableName
     * @param async
     * @param pageable
     * @param model
     * @return
     */
    @GetMapping("/{teacherNumber}")
    public String listConsumableStorageByTeacher(@PathVariable("teacherNumber")String teacherNumber,
                                                 @RequestParam(value = "consumableName",required = false,defaultValue = "") String consumableName,
                                                 @RequestParam(value = "async",required = false) boolean async,
                                                 Pageable pageable,
                                                 Model model)throws Exception{
        TeacherInfo teacher= (TeacherInfo) userDetailsService.loadUserByUsername(teacherNumber);
        Page<ConsumableStorage> consumableStoragePage=consumableStorageService.listConsumableStorageByTeacherAndConsumableName(consumableName,teacher,pageable);
        List<ConsumableStorage> consumableStorages=consumableStoragePage.getContent();
        model.addAttribute("page",consumableStoragePage);
        model.addAttribute("consumableStorages",consumableStorages);
        return async==true?"/consumables/storages ::#mainContainerRepleace":"/consumables/storages";
    }

    /**
     * 撤销待审核耗材入库（删除）
     * @param consumableStorageId
     * @param teacherNumber
     * @return
     * @throws Exception
     */
    @ApiOperation(value ="撤销待审核的耗材入库申请（个人）" )
    @ResponseBody
    @DeleteMapping("/{teacherNumber}/revocation/{consumableStorageId}")
    @PreAuthorize("authentication.name.equals(#teacherNumber)")
    public ResponseData delete(@PathVariable("consumableStorageId") Long consumableStorageId,
                               @PathVariable("teacherNumber") String teacherNumber)throws Exception{
        consumableStorageService.remove(consumableStorageId);
        return new ResponseData(true,"撤销成功！");
    }

    /**
     * 完成已通过的耗材入库
     * @param consumableStorageId
     * @param teacherNumber
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "完成已通过的耗材入库")
    @ResponseBody
    @PostMapping("/{teacherNumber}/achieve/{consumableStorageId}")
    @PreAuthorize("authentication.name.equals(#teacherNumber)")
    public ResponseData achieve(@PathVariable("consumableStorageId") Long consumableStorageId,
                                @PathVariable("teacherNumber") String teacherNumber)throws Exception{
        consumableStorageService.achieve(consumableStorageId);
        return new ResponseData(true,"耗材入库成功！");
    }
}
